<?php

namespace app\admin\controller;

use app\api\service\Token as TokenService;
use app\admin\model\Operator;

use think\Controller;
use think\Request;

class Base extends Controller
{

    const MSG_SUCCESS = '请求成功';
    const MSG_ERROR = '请求失败';
    const NOT_DATA = '暂无数据';
    const NOT_PARAM = '缺少参数';

    const WX_FEE = 0.006; // 微信收取费用

    /**
     * 后台登录管理员信息
     */
    protected $admin = '';

    protected $model = ''; // Model

    // 店铺ID
    //protected $storeId = '';

    public function _initialize()
    {
        parent::_initialize();

        $module = $this->request->module();
        $controller = $this->request->controller();
        $action = $this->request->action();

        if(($controller != 'Auth' || $action != 'doLogin') && $controller != 'Faq'){
            // Check Auth
            $this->authCheck();
        }
    }

    /**
     * Check auth
     */
    private function authCheck()
    {
        //$session_admin = session('ADMIN');
        $session_admin = cookie('ADMIN');

        /*if(empty($session_admin->id)){
            //return show(config('status.ERROR_STATUS'), self::MSG_ERROR, '请先登录！');
            $return = [
                'status' => config('status.SUCCESS_STATUS'),
                'message' => self::MSG_ERROR,
                'data' => '请先登录！'
            ];
            echo json_encode($return, JSON_UNESCAPED_UNICODE);
            exit;
        }*/

        $param = Request::instance()->param();
        if(empty($param['userId'])){
            $return = [
                'status' => config('status.ERROR_STATUS'),
                'message' => self::MSG_ERROR,
                'data' => '请先登录！'
            ];
            echo json_encode($return, JSON_UNESCAPED_UNICODE);
            exit;
        }
        $adminInfo = Operator::where(['id' => $param['userId']])->find();
        if(!$adminInfo){
            $return = [
                'status' => config('status.ERROR_STATUS'),
                'message' => self::MSG_ERROR,
                'data' => '该用户不存在！'
            ];
            echo json_encode($return, JSON_UNESCAPED_UNICODE);
            exit;
        }
        // 管理员权限
        // 是否是超级管理员
        if($adminInfo['roleType'] == 1){
            $ruleIds = db('module')
                ->where(['isActive' => 1, 'status' => ['in', '1,2']])
                ->field('id as moduleId, name')
                ->select();
        }else{
            $ruleIds = db('operatormodule')
                ->alias('a')
                ->where(['a.operateId' => $adminInfo['id']])
                ->join('__MODULE__ m', 'm.id = a.moduleId')
                ->field('a.moduleId, m.name')
                ->select();
        }
        $adminInfo['ruleIds'] = $ruleIds;

        if(!$this->checkAccess()){
            $return = [
                'status' => config('status.ERROR_STATUS'),
                'message' => self::MSG_ERROR,
                'data' => '您没有访问权限！'
            ];
            echo json_encode($return, JSON_UNESCAPED_UNICODE);
            exit;
        }

        //$this->admin = $session_admin;
        $this->admin = $adminInfo;
    }

    /**
     * 权限检查
     * @return bool
     */
    private function checkAccess() {
        // TODO
        return true;
    }

    /**
     * 默认条件
     */
    public function getDefaultWhere() {
        $defaultWhere = [];
        switch ($this->admin->roleType) {
            case 1:
                $defaultWhere = [];
                break;
            case 2:
                /*$defaultWhere = [
                    //'province' => $this->admin->province,
                    'city' => $this->admin->city,
                    //'area' => $this->admin->area
                ];*/
                if($this->admin->city == '所有') {
                    $defaultWhere = [
                        'province' => $this->admin->province
                    ];
                } else {
                    $defaultWhere = [
                        'province' => $this->admin->province,
                        'city' => $this->admin->city
                    ];
                }
                break;
            default:
                $defaultWhere = [];
                break;
        }

        return $defaultWhere;
    }

    public function getHttpParam()
    {
        $param = json_encode(Request::instance()->param());

        return json_decode($param);
    }


    protected function checkPrimaryScope()
    {
        TokenService::needPrimaryScope();

    }

    protected function checkExclusiveScope()
    {
        TokenService::needExclusiveScope();
    }

}